package com.three.crm.page.company;


import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.three.crm.R;
import com.three.crm.adapter.NinePictureAdapter;
import com.three.crm.base.BaseActivity;
import com.three.crm.bean.CompanyBean;
import com.three.crm.bean.FileBean;
import com.three.crm.constant.AppConstant;
import com.three.crm.http.CommonResultCallBack;
import com.three.crm.page.VideoPlayActivity;
import com.three.crm.require.AppRequireService;
import com.three.crm.util.AppManager;
import com.three.crm.util.GsonUtils;
import com.three.crm.util.SignUtil;
import com.three.crm.util.TString;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 伙伴详情
 * created by lqd on 2024-01-05.
 */
public class CompanyDetailActivity extends BaseActivity {

    private int id;
    private TextView tvTitle;
    private EditText etCompany,etPhone,etAddress,etLabel,etRemark,etProvince,etCity,etArea,etPhone2,etProduction;
    private ImageView ivVideo,ivLogo;
    private FrameLayout flVideo,flLogo;
    private RecyclerView recyclerView,rvCompany;
    private NinePictureAdapter adapter,companyImgAdapter;
    private String uploadVideoPath = "";

    @Override
    protected void initParms(Bundle bundle) {
        if (bundle != null) {
            id = bundle.getInt(AppConstant.EXTRAS_DATA);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_company_detail;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText(R.string.form_detail);

        etCompany = $(R.id.et_customer);
        etPhone = $(R.id.et_phone);
        etAddress = $(R.id.et_address);
        etLabel = $(R.id.et_label);
        etRemark = $(R.id.et_remark);
        etProvince = $(R.id.et_province);
        etCity = $(R.id.et_city);
        etArea = $(R.id.et_area);

        etPhone2 = $(R.id.et_phone2);
        etProduction = $(R.id.et_production);
        ivVideo = $(R.id.iv_video);
        flVideo = $(R.id.fl_video);
        ivLogo = $(R.id.iv_logo);
        flLogo = $(R.id.fl_logo);

        recyclerView = $(R.id.rv_picture);
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new NinePictureAdapter();
        adapter.setType("detail");
        recyclerView.setAdapter(adapter);

        rvCompany = $(R.id.rv_picture_company);
        GridLayoutManager layoutManager2 = new GridLayoutManager(this,3);
        rvCompany.setLayoutManager(layoutManager2);
        companyImgAdapter = new NinePictureAdapter();
        companyImgAdapter.setType("detail");
        rvCompany.setAdapter(companyImgAdapter);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        $(R.id.fl_video).setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
            case R.id.fl_video:
                onPlayVideo();
                break;
        }
    }

    private void onPlayVideo() {
        if (TextUtils.isEmpty(uploadVideoPath)) {
            showToast("视频不存在了");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.VIDEO_PATH,uploadVideoPath);
        startActivity(VideoPlayActivity.class,bundle);
    }

    @Override
    protected void initData(Context mContext) {
        queryDetail();
    }

    private void queryDetail() {
        if (id != 0) {
            showLoading(getString(R.string.loading));
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("userToken", AppManager.getToken());
                jsonObject.put("company_id",id);
                jsonObject.put("timestamp",getTimestamp());
                String sign = SignUtil.genSign(jsonObject);
                jsonObject.put("sign",sign);
                AppRequireService.queryCompanyDetail(this, AppConstant.QUERY_DETAIL, jsonObject, new CommonResultCallBack(this) {
                    @Override
                    public void onResult(String result, int requireCode) {
                        Log.i("TAG","result="+result);
                        hideLoading();
                        if (requireCode == AppConstant.QUERY_DETAIL) {
                            CompanyBean bean = GsonUtils.parseJsonWithGson(result, CompanyBean.class);
                            setDetail(bean);
                        }
                    }

                    @Override
                    public void onConnectTimeOut(Call call, Exception e, int requireCode) {
                        hideLoading();
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onFail(String msg, int requireCode) {
                        hideLoading();
                        showToast(msg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void setDetail(CompanyBean bean) {
        if (bean != null) {
           etCompany.setText(bean.getTitle());
           etPhone.setText(bean.getPhone());
           etProvince.setText("" + bean.getProvince());
           etCity.setText("" + bean.getCity());
           etArea.setText("" + bean.getArea());
           etAddress.setText(bean.getAddress());
           etLabel.setText(bean.getLabel());
           etRemark.setText(bean.getContent());
           etPhone2.setText(bean.getPhone2());
           etProduction.setText(bean.getMonth_production());

//            String logoUrl = bean.getLogo();
//            if (TextUtils.isEmpty(logoUrl)) {
//                flLogo.setVisibility(View.GONE);
//            } else {
//                flLogo.setVisibility(View.VISIBLE);
//                Glide.with(this)
//                        .load(logoUrl)
//                        .centerCrop()
//                        .placeholder(R.mipmap.ic_default_image)
//                        .diskCacheStrategy(DiskCacheStrategy.ALL)
//                        .into(ivLogo);
//            }

            String logoArr = bean.getLogo();
            String[] logos = logoArr.split(",");
            if (logos != null && logos.length > 0) {
                List<FileBean> companyPictureList = new ArrayList<>();
                for (int i = 0; i < logos.length; i++) {
                    String url = logos[i];
                    FileBean bean2 = new FileBean();
                    bean2.setUrl(url);
                    companyPictureList.add(bean2);
                }
                companyImgAdapter.setNewData(companyPictureList);
            }

            String imgArr = bean.getImages();
            String[] imgs = imgArr.split(",");
            if (imgs != null && imgs.length > 0) {
                List<FileBean> list = new ArrayList<>();
                for (int i = 0; i < imgs.length; i++) {
                    String url = imgs[i];
                    FileBean fileBean = new FileBean();
                    fileBean.setUrl(url);
                    list.add(fileBean);
                }
                adapter.setNewData(list);
            }

            uploadVideoPath = bean.getVideo_url();
            if (TString.isEmpty(uploadVideoPath)) {
                flVideo.setVisibility(View.GONE);
            } else {
                flVideo.setVisibility(View.VISIBLE);
                Glide.with(this)
                        .load(uploadVideoPath)
                        .centerCrop()
                        .placeholder(R.mipmap.ic_default_image)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(ivVideo);
            }
        }
    }
}